import * as types from './mutations-types'
import { shuffle } from 'common/js/tool'
import { playMode } from 'common/js/config'
import {
  saveSearch,
  loadSearch,
  deleteSearch,
  clearSearch,
  savePlayHistory,
  saveFavoriteList,
  deleteFavoriteList
} from 'common/js/cache'
import * as _ from 'lodash'

export default {
  selectPlay({ commit, state }, { list, index }) {
    // commit(types.SET_FULL_SCREEN, true)
    commit(types.SET_SEQUENCE_LIST, list)
    if (state.mode === playMode.random) {
      let randomList = shuffle(list)
      commit(types.SET_PLAYLIST, randomList)
      index = _.findIndex(randomList, function(o) { return o.id === list[index] })
    } else {
      commit(types.SET_PLAYLIST, list)
    }
    commit(types.SET_FULL_SCREEN, true)
    commit(types.SET_CURRENT_INDEX, index)
    commit(types.SET_PLAYING_STATE, true)
  },
  randomPlay({ commit }, { list }) {
    commit(types.SET_SEQUENCE_LIST, list)
    let newList = shuffle(list)
    commit(types.SET_PLAYLIST, newList)
    commit(types.SET_PLAY_MODE, playMode.random)
    commit(types.SET_CURRENT_INDEX, 0)
    commit(types.SET_PLAYING_STATE, true)
  },
  insertSong({ commit, state }, song) {
    // 修改 playlist
    let playlist = state.playlist.slice()
    let currentIndex = state.currentIndex
    // 检查列表中是否有 歌曲与新加的歌曲重复
    let repeatIndex = _.findIndex(playlist, function(o) { return o.id === song.id })
    currentIndex = currentIndex + 1
    playlist.splice(currentIndex, 0, song)
    if (repeatIndex > -1) {
      if (repeatIndex < currentIndex) {
        playlist.splice(repeatIndex, 1)
        currentIndex = currentIndex - 1
      } else {
        playlist.splice(repeatIndex + 1, 1)
      }
    }

    // 修改 sequenceList
    let currentIndex2 = state.currentIndex
    let sequenceList = state.sequenceList.slice()
    let repeatIndex2 = _.findIndex(sequenceList, function(o) { return o.id === song.id })
    currentIndex2 = currentIndex2 + 1
    sequenceList.splice(currentIndex2, 0, song)
    if (repeatIndex2 > -1) {
      if (repeatIndex2 < currentIndex2) {
        sequenceList.splice(repeatIndex2, 1)
        currentIndex2 = currentIndex2 - 1
      } else {
        sequenceList.splice(repeatIndex2 + 1, 1)
      }
    }
    commit(types.SET_PLAYLIST, playlist)
    commit(types.SET_SEQUENCE_LIST, sequenceList)
    commit(types.SET_CURRENT_INDEX, currentIndex)
    commit(types.SET_PLAYING_STATE, true)
  },
  deleteSong({ commit, state }, song) {
    // 修改 playlist
    let playlist = state.playlist.slice()
    let sequenceList = state.sequenceList.slice()
    let currentIndex = state.currentIndex
    let index = _.findIndex(playlist, function(o) { return o.id === song.id })
    if (index > -1) playlist.splice(index, 1)
    let index2 = _.findIndex(sequenceList, function(o) { return o.id === song.id })
    if (index2 > -1) sequenceList.splice(index2, 1)

    if (!playlist.length) {
      commit(types.SET_PLAYING_STATE, false)
    } else {
      commit(types.SET_PLAYING_STATE, true)
    }
    if (index < currentIndex) {
      currentIndex--
    }
    commit(types.SET_PLAYLIST, playlist)
    commit(types.SET_SEQUENCE_LIST, sequenceList)
    commit(types.SET_CURRENT_INDEX, currentIndex)
  },
  saveSearchHistory({ commit, state }, query) {
    saveSearch(query)
    commit(types.SET_SEARCHHISTORY, loadSearch())
  },
  deleteSearchHistory({ commit }, query) {
    commit(types.SET_SEARCHHISTORY, deleteSearch(query))
  },
  clearSearchHistory({ commit }) {
    commit(types.SET_SEARCHHISTORY, clearSearch())
  },
  clearSongs({ commit }) {
    commit(types.SET_PLAYLIST, [])
    commit(types.SET_SEQUENCE_LIST, [])
    commit(types.SET_CURRENT_INDEX, -1)
    commit(types.SET_PLAYING_STATE, false)
  },
  saveSong({ commit }, song) {
    commit(types.SET_PLAYHISTORY, savePlayHistory(song))
  },
  saveFavoriteList({ commit }, song) {
    commit(types.SET_FAVORITELIST, saveFavoriteList(song))
  },
  deleteFavoriteList({ commit }, song) {
    commit(types.SET_FAVORITELIST, deleteFavoriteList(song))
  }
}
